package com.hitemole.view;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.hitemole.levelObject.ComboBar;
import com.hitemole.levelObject.Moles;
import com.hitemole.manager.GameManager;
import com.hitemole.manager.GameManager.GAME_STATES;
import com.hitemole.objects.GameObject;

public class GameWorldRenderer {

	private OrthographicCamera cam;
	private GameManager world;

	private ShapeRenderer debugRenderer = new ShapeRenderer();

	private SpriteBatch spriteBatch;
	private float xScale;
	private float yScale;
	private boolean debug;

	public GameWorldRenderer(GameManager world, boolean debug) {
		// TODO Auto-generated constructor stub
		this.world = world;
		this.debug = debug;
		spriteBatch = new SpriteBatch();
	}

	public void render() {
		// TODO Auto-generated method stub
		// set viewport
		spriteBatch.setProjectionMatrix(cam.combined);
		spriteBatch.begin();
			drawBackground();
			drawMoles();
			drawGUI();
			drawFont();
			if(world.getGameState().equals(GAME_STATES.GAME_START)){
				world.getDefaultBigFont().draw(spriteBatch, world.countDown, Gdx.graphics.getWidth()/2-(40*xScale),Gdx.graphics.getHeight()/2-(150*yScale));
			}
			if(world.getGameState().equals(GAME_STATES.GAME_END)){
				world.getDefaultBigFont().draw(spriteBatch, "GAME", Gdx.graphics.getWidth()/2-(230*xScale), Gdx.graphics.getHeight()/2-(230*yScale));
				world.getDefaultBigFont().draw(spriteBatch, "OVER", Gdx.graphics.getWidth()/2-(230*xScale), Gdx.graphics.getHeight()/2-(100*yScale));
			}
		spriteBatch.end();
		if(world.getGameState().equals(GAME_STATES.GAME_PAUSE)){
			world.getStageUI().draw();
		}
		if (debug) {
			drawDebug();
		}
	}

	private void drawGUI() {
		// TODO Auto-generated method stub
		ComboBar cb = world.getComboBar();
		GameObject cbf= world.getComboBarFrame();
		GameObject pb=world.getPauseButton();
		cb.draw(spriteBatch,xScale,yScale);
		spriteBatch.draw(pb.getImage(),pb.getPosition().x*xScale,pb.getPosition().y*yScale,pb.getImage().getRegionWidth()*xScale,pb.getImage().getRegionHeight()*yScale);
		spriteBatch.draw(cbf.getImage(), cbf.getPosition().x*xScale, cbf.getPosition().y*yScale,cbf.getImage().getRegionWidth()*xScale,cbf.getImage().getRegionHeight()*yScale);
		if (world.getHammer().getDrawAnimation() && !world.getGameState().equals(GAME_STATES.GAME_END)) {
			spriteBatch.draw(world.getHammer().getCurrentAnimationFrame(),
					world.getHammer().getRelativePosition().x, world
							.getHammer().getRelativePosition().y, world
							.getHammer().getRelativeSize().x, world.getHammer()
							.getRelativeSize().y);
		}
		Color c = spriteBatch.getColor();
		for(int i=0;i<3;i++){	
			spriteBatch.setColor(c.r,c.g,c.b,0.3f);
			spriteBatch.draw(world.getLivesTexture(),(300+(i*25))*xScale,60*yScale,(world.getLivesTexture().getRegionWidth()*0.4f)*xScale,(world.getLivesTexture().getRegionHeight()*0.4f)*yScale);
			spriteBatch.setColor(c);
			if(world.getLives()-i>0)
			spriteBatch.draw(world.getLivesTexture(),(300+(i*25))*xScale,60*yScale,(world.getLivesTexture().getRegionWidth()*0.4f)*xScale,(world.getLivesTexture().getRegionHeight()*0.4f)*yScale);
		}
	}

	private void drawFont() {
		// TODO Auto-generated method stub
		// font.draw(spriteBatch, "Score:", 80*xScale, 20*yScale);
		world.getDefaultSmallFont().draw(spriteBatch, world.getScores().getStringScore(), 95 * xScale,
				18 * yScale);
		world.getDefaultSmallFont().draw(spriteBatch, "x" + world.getScores().getMultiplier(),
				334 * xScale, 18 * yScale);
	}

	private void drawMoles() {
		// TODO Auto-generated method stub
		for (Moles mole : world.getMoles()) {
			TextureRegion molesFrame = mole.getCurrentAnimationFrame();
			spriteBatch.draw(molesFrame, mole.getRelativePosition().x,
					mole.getRelativePosition().y, mole.getRelativeSize().x,
					mole.getRelativeSize().y);

		}
	}

	private void drawBackground() {
		// TODO Auto-generated method stub
		spriteBatch.draw(world.getBackground().getImage(), world
				.getBackground().getPosition().x, world.getBackground()
				.getPosition().y, world.getBackground().getImage()
				.getRegionWidth()
				* xScale, world.getBackground().getImage().getRegionHeight()
				* yScale);
		spriteBatch.draw(world.getArcadeTable().getImage(), world
				.getArcadeTable().getPosition().x, world.getArcadeTable()
				.getPosition().y, world.getArcadeTable().getImage()
				.getRegionWidth()
				* xScale, world.getArcadeTable().getImage().getRegionHeight()
				* yScale);
	}

	public void setSize(int width, int height) {
		// TODO Auto-generated method stub
		// this.width=width;
		// this.height=height;
		this.cam = new OrthographicCamera(width, height);
		this.cam.position.set(width / 2, height / 2, 0);
		this.cam.setToOrtho(true, width, height);
		this.cam.update();
		xScale = (float) width / GameManager.DEFAULT_SCREEN_WIDTH;
		yScale = (float) height / GameManager.DEFAULT_SCREEN_HEIGHT;
		for (Moles mole : world.getMoles()) {
			mole.setScale(xScale, yScale);
		}
		world.getPauseButton().setScale(xScale,yScale);
		world.getHammer().setScale(xScale, yScale);
		world.getDefaultSmallFont().setScale(xScale, yScale);
		world.getDefaultBigFont().setScale(xScale*4, yScale*4);
		world.setStageUI(new Stage(GameManager.DEFAULT_SCREEN_WIDTH, GameManager.DEFAULT_SCREEN_WIDTH,false));
		world.getStageUI().setViewport(GameManager.DEFAULT_SCREEN_WIDTH, GameManager.DEFAULT_SCREEN_HEIGHT,false);
		world.getStageUI().addActor(world.getTable());
	}

	private void drawDebug() {
		// render blocks
		debugRenderer.setProjectionMatrix(cam.combined);
		debugRenderer.begin(ShapeType.Line);
		for (Moles mole : world.getMoles()) {
			Rectangle rect = mole.getRelativeCollisionShape();
			debugRenderer.setColor(new Color(0, 1, 0, 1));
			debugRenderer.rect(rect.x, rect.y, rect.width, rect.height);
		}
		Rectangle rect = world.getPauseButton().getRelativeCollisionShape();
		debugRenderer.rect(rect.x,rect.y,rect.width,rect.height);
		debugRenderer.end();
	}

}
